package com.example.siyun.controller.permissionscontroller;

import com.example.siyun.pojo.EnumPojo;
import com.example.siyun.pojo.PermissionsTable;
import com.example.siyun.service.permissionsservice.PermissionsTableService;
import com.example.siyun.util.PageResult;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
 * @Auther: 全
 * @Date: 2022/9/26 09:33
 * @Description:权限管理模块权限列表
 */
@Slf4j
@RestController
public class PermissionsTableController {
    @Autowired
    private PermissionsTableService permissionsTableService;
    /**
     * 获取权限列表的信息
     * @return 返回权限列表信息
     */
    @RequestMapping("/advanced/getpermissionstable")
    public PageResult getAll(PermissionsTable permissionsTable, int page, int limit) {
        log.info("获取权限列表的信息");
        Map<String, Object> map = new HashMap<>();
        PageHelper.startPage(page, limit);
        List<PermissionsTable> list = permissionsTableService.selectPermissionsTable(permissionsTable, EnumPojo.VALUE_DEL_0);
        PageInfo<PermissionsTable> pageInfo = new PageInfo<>(list);
        PageResult pageResult=new PageResult();
        pageResult.setState(0);
        pageResult.setMessage("成功");
        pageResult.setTotal(pageInfo.getTotal());
        pageResult.setData(pageInfo.getList());
        log.info("获取权限列表的信息成功");
        return pageResult;
    }
    /**
     * 通过id删除对应的管理人员
     * @return 返回台管理人员列表的数据
     */
    @RequestMapping("/advanced/delpermissionstable")
    public PageResult delCourseTable(int id){
        log.info("通过id删除对应的管理人员");
        permissionsTableService.delPermissionsTable(id,EnumPojo.VALUE_DEL_1);
        PageResult pageResult=new PageResult();
//        pageResult.setStatus(200);
        log.info("通过id删除对应的管理人员成功");
        return pageResult;
    }
    /**
     * 通过id修改对应的账号状态
     */
    @RequestMapping("/advanced/updatepermissenable")
    public PageResult updatePermissEnable(int id,int status){
        log.info("通过id修改对应的账号状态");
        permissionsTableService.updateIsEnable(id,status);
        PageResult pageResult=new PageResult();
//        pageResult.setStatus(200);
        log.info("通过id修改对应的账号状态成功");
        return pageResult;
    }
    @RequestMapping("/advanced/selectpermissionsbyid")
    public PageResult selectPermissionsById(int id){
        PermissionsTable permissionsTable=permissionsTableService.selePerById(id);
        PageResult pageResult=new PageResult();
//        pageResult.setStatus(200);
        pageResult.setData(permissionsTable);
        return pageResult;
    }
    /**
     * 通过id查询对应的管理人员信息
     * @return 返回查询出来的管理人员信息
     */
    @RequestMapping("/advanced/updatapermissionstable")
    public PageResult updataPermissionsTable(PermissionsTable permissionsTable){
        log.info("通过id查询对应的管理人员信息");
        DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String time=permissionsTable.getTime();
        format.setLenient(false);
        try {
            Timestamp ts1 = new Timestamp(format.parse(time).getTime());
            permissionsTable.setCreatetime(ts1);
            System.out.println(permissionsTable);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        permissionsTableService.updateOerMissions(permissionsTable);
        PageResult pageResult=new PageResult();
//        pageResult.setStatus(200);
        log.info("通过id查询对应的管理人员信息成功");
        return pageResult;

    }
    @RequestMapping("/advanced/addpermissionstable")
    public PageResult addPermissionsTable(PermissionsTable permissionsTable, HttpSession session){
        Timestamp time = new Timestamp(new Date().getTime());
        String loginame = (String) session.getAttribute("loginname");
        permissionsTable.setCreatetime(time);
        permissionsTable.setAdminname(loginame);
        permissionsTableService.addPermissionsTable(permissionsTable,EnumPojo.VALUE_DEL_0);
        PageResult pageResult=new PageResult();
//        pageResult.setStatus(200);
        return pageResult;
    }
}
